<?php

namespace common\components;

use common\helpers\IpLocation\IpLocation;
use common\helpers\UserAgent\Agent;
use common\models\OperLog;
use Yii;
use yii\base\ActionEvent;
use yii\base\Behavior;
use yii\base\Controller;
/**
 * 后台操作记录
 * Class OperBehavior
 * @package common\components
 */
class OperBehavior extends Behavior
{
    public function events()
    {
        return [
//            Controller::EVENT_BEFORE_ACTION => 'beforeAction',
            Controller::EVENT_AFTER_ACTION => 'afterAction',
        ];
    }

    /**
     * @param ActionEvent $event
     * @return bool
     */
    public function afterAction($event)
    {
        $controller_name = $event->action->controller->id;
        $action_name = $event->action->id;
        $permission = 'admin:' . $controller_name.':'.$action_name;
        $notAuthController = ['public', 'common','oper-log'];
        $notAuthAction = ['tree'];
        $notAuthPermission=['admin:index:index','admin:index:home','admin:public:login'];
        if (in_array($controller_name, $notAuthController) || in_array($action_name, $notAuthAction) || in_array($permission,$notAuthPermission)) {
            return true;
        }
        $ipLocation = new IpLocation();
        $location = $ipLocation->getlocation(Yii::$app->getRequest()->getUserIP());
        $login_location = '';
        if ($location) {
            if ($location['country']) {
                $login_location = iconv('GBK', 'UTF-8', $location['country']);
            }
        }
        $agent = new Agent();
        $os = $agent->platform() . ' ' . $agent->version($agent->platform());
        $browser = $agent->browser() . ' ' . $agent->version($agent->browser());
        $log = new OperLog();
        $log->module = 'admin';
        $log->controller = $controller_name;
        $log->action = $action_name;
        $log->request_method = \Yii::$app->request->getMethod();
        $log->auid = isset(Yii::$app->user) && !empty(\Yii::$app->user->id) ? \Yii::$app->user->id : '';
        $log->oper_url = Yii::$app->request->pathInfo;
        $log->oper_ip = $login_location;
        $log->oper_agent = Yii::$app->request->getUserAgent();
        $log->os = $os;
        $log->browser = $browser;
        $log->menu_perms = $permission;
        if (Yii::$app->request->getIsGet()) {
            //get请求
            $requestData = Yii::$app->request->getQueryParams();
        } else {
            $rawData = Yii::$app->request->getRawBody();
            mb_parse_str($rawData, $requestData);
        }
        if (!empty($requestData)) {
            $log->oper_param = json_encode($requestData, JSON_PRETTY_PRINT);
        } else {
            $log->oper_param = '';
        }

        if (Yii::$app->request->getIsAjax()) {
            $data = $event->action->controller->module->response->data;
            if (isset($data['success'])) {
                if ($data['success'] == true) {
                    $log->status = 1;
                } else {
                    $log->status = 0;
                    $log->error_msg = isset($data['msg']) ? $data['msg'] : '';
                }
            } else {
                $log->status = 1;
            }
            $log->json_result = json_encode($data, JSON_PRETTY_PRINT);
        } else {
            $log->json_result = '';
            $log->status = Yii::$app->response->getStatusCode()==200?1:0;
        }
        $log->save(false);

        return true;
    }
}
